﻿using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace DevelopTool.Forms.ImageToolForms
{
    /// <summary>
    /// 图片浏览器
    /// </summary>
    public partial class ImageViewForm : BaseUIPage
    {

        private string folderPath;

        /// <summary>
        /// 目录路径
        /// </summary>
        public string FolderPath
        {
            get
            {
                return this.folderPath;
            }
            set
            {
                this.folderPath = value;
            }
        }

        /// <summary>
        /// 已选择的图片集合
        /// </summary>
        private readonly List<PictureBox> selectedPictureBoxs = new List<PictureBox>();


        /// <summary>
        /// 放大/缩小步长
        /// </summary>
        private readonly int sizeStep = 40;


        public ImageViewForm()
        {
            InitializeComponent();
        }

        private void ImageViewForm_Load(object sender,EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.folderPath))
            {
                this.txtFolderPath.Text = this.folderPath;
                ShowImages(this.folderPath);
            }
        }


        /// <summary>
        /// 打开目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOpenFolder_Click(object sender,EventArgs e)
        {
            string folderPath = Setting.Tool.ImageView_LastFolderPath;
            if(Dialogs.SelectDirEx("请选择图片目录",ref folderPath))
            {
                Setting.Tool.ImageView_LastFolderPath = folderPath;//记录下打开的目录，方便下次直接选择

                this.topPnl.Text = folderPath;

                ShowImages(folderPath);
            }
        }

        private void txtFolderPath_DoEnter(object sender,EventArgs e)
        {
            ShowImages(this.txtFolderPath.Text);
        }


        /// <summary>
        /// 显示图片
        /// </summary>
        private void ShowImages(string folderPath)
        {
            if(string.IsNullOrEmpty(folderPath))
            {
                this.ShowWarningNotifier("请先选择目录");
                return;
            }

            if(!Directory.Exists(folderPath))
            {
                this.ShowWarningNotifier("目录不存在");
                return;
            }

            List<string> files = Directory.GetFiles(folderPath,"*",SearchOption.AllDirectories).OrderBy(it => it).ToList();

            this.imageFlowLayoutPanel.Clear();

            foreach(string file in files)
            {
                try
                {
                    Image image = Image.FromFile(file);
                    PictureBox pictureBox = new PictureBox()
                    {
                        Image = image,
                        SizeMode = PictureBoxSizeMode.StretchImage,
                        Size = new Size(Convert.ToInt32(image.Width / 2),Convert.ToInt32(image.Height / 2)),
                        BorderStyle = BorderStyle.FixedSingle,
                        Tag = file,
                        Cursor = Cursors.Hand,
                    };
                    pictureBox.MouseClick += PictureBox_MouseClick;

                    this.imageFlowLayoutPanel.Add(pictureBox);

                    uiToolTip.SetToolTip(pictureBox,file);

                }
                catch(Exception) { }
            }
        }


        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PictureBox_MouseClick(object sender,MouseEventArgs e)
        {
            if(e.Button != MouseButtons.Left) return;

            if(!(sender is PictureBox pictureBox)) return;

            var picBox = this.selectedPictureBoxs.FirstOrDefault(s => s.Tag.ToString() == pictureBox.Tag.ToString());

            if(picBox != null)
            {
                this.selectedPictureBoxs.Remove(picBox);
                picBox.Controls.Clear();
                picBox.BorderStyle = BorderStyle.FixedSingle;
            }
            else
            {
                UISymbolButton button = new UISymbolButton()
                {
                    Symbol = 61452,
                    StyleCustomMode = true,
                    Style = UIStyle.Red,
                    Size = new Size(60,40),
                };
                pictureBox.Controls.Add(button);
                pictureBox.BorderStyle = BorderStyle.Fixed3D;

                this.selectedPictureBoxs.Add(pictureBox);
            }
            this.btnDelete.Text = $"删除（{this.selectedPictureBoxs.Count}）";
        }



        /// <summary>
        /// 放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnListBig_Click(object sender,EventArgs e)
        {
            this.btnListBig.Enabled = false;
            foreach(Control control in this.imageFlowLayoutPanel.Panel.Controls)
            {
                int width = control.Width;
                int height = control.Height;

                control.Size = new Size(width + sizeStep,height + sizeStep);
            }
            this.btnListBig.Enabled = true;
        }


        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnListSmall_Click(object sender,EventArgs e)
        {
            this.btnListSmall.Enabled = false;
            foreach(Control control in this.imageFlowLayoutPanel.Panel.Controls)
            {
                PictureBox pictureBox = control as PictureBox;
                int width = pictureBox.Width;
                int height = pictureBox.Height;

                pictureBox.Size = new Size(width - sizeStep,height - sizeStep);
            }
            this.btnListSmall.Enabled = true;
        }



        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender,EventArgs e)
        {
            if(this.selectedPictureBoxs.Count <= 0) return;

            if(this.ShowAskDialog($"{GlobalData.AppName_zh}-重要提示",$"是否确定删除附件{this.selectedPictureBoxs.Count}个文件，删除后无法恢复！",UIStyle.Orange))
            {
                foreach(PictureBox pictureBox in this.selectedPictureBoxs)
                {
                    this.imageFlowLayoutPanel.Remove(pictureBox);
                    pictureBox.Image.Dispose();
                    File.Delete(pictureBox.Tag.ToString());
                }
                this.selectedPictureBoxs.Clear();
                this.btnDelete.Text = $"删除（{this.selectedPictureBoxs.Count}）";
                this.ShowSuccessNotifier("删除完成");
            }
        }


    }
}
